﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BinAnalysisHelp
{

    public enum SettingFileType
    {
        IQ_Common,  //2
        IQ_Config,  //2
        PLL,        //3
        ISP_Common,  //3
        ISP_Config,  //3
        Sensor_Config,  //3
        Sensor_Common,  //3

    }
    public struct HeadStruct
    {
        public List<byte> Key;
        public long Addr;
        public long Length;
    }
    public class Size
    {
        public int Width { get; set; }
        public int Height { get; set; }
    }

    public class Setting1_31Data
    {
        public List<byte> Item1;
        public List<byte> Item2;
    }
    public class SettingFileIDInfo
    {
        public SettingFileType Type { get; set; }
        public int width;
        public int height;
        public int ID { get; set; }
        /// <summary>
        /// 这个用来区分在哪个区的
        /// </summary>
        public int RegionID { get; set; }

        /// <summary>
        /// 这个用来区分Common的的
        /// </summary>
        public int IspClock { get; set; }


        /// <summary>
        /// 用于软件查找坐在区域的setting文件存放的位置信息
        /// </summary>
        public string note { get { return $"setting区{RegionID}_{ID.ToString().PadLeft(2,'0')}"; } }
        //public SettingFileIDInfo(SettingFileType settingFileType, int id, Size SizeInfo )
        //    {
        //    Type = settingFileType;
        //    ID = id;
        //    size = SizeInfo;
        //}
        //public SettingFileIDInfo()
        //{

        //}
    }

    public class Setting1_31Parser
    {
        public List<Setting1_31Data> DataList = new List<Setting1_31Data>();

        public void  LoadSettingData(List<byte> data)
        {
            //清空数据
            DataList = new List<Setting1_31Data>();
            var headList = LoadHeadData(data.GetRange(0, 128));
            foreach (var item in headList)
            {
                Setting1_31Data TheTuple = new Setting1_31Data();
                TheTuple.Item1 = item.Key;
                TheTuple.Item2 = data.GetRange((int)item.Addr, (int)item.Length);
                DataList.Add(TheTuple);
            }
        }

        public List<HeadStruct> LoadHeadData(List<byte> HeadData)
        {
            List<HeadStruct> Out = new List<HeadStruct>();
            //补齐未到达128个数据的情况
            for (int i = HeadData.Count; i < 128; i++)
            {
                HeadData.Add(0x0);
            }

            for (int i = 0; i < 128; i+=16)
            {
                
                HeadStruct Head = new HeadStruct();
                Head.Key = HeadData.GetRange(i, 8);
                if (Head.Key.All(x => x == 0 || x == 0xff))
                {
                    continue;
                }
                Head.Addr = BitConverter.ToUInt32(HeadData.GetRange(i + 8, 4).ToArray(),0);
                Head.Length = BitConverter.ToUInt32(HeadData.GetRange(i + 12, 4).ToArray(), 0); 
                Out.Add(Head);
            }

            return Out;

        }

        /// <summary>
        /// 这个是解析外部调用解析e方法
        /// 24.1.31 经过讨论这里出现的版本号信息导致 解析不了 则报错
        /// </summary>
        /// <returns></returns>
        public List<SettingFileIDInfo> GetFileIDInfo()
        {

            List<SettingFileIDInfo> Out = new List<SettingFileIDInfo>();
            if (DataList.Count <= 0)
            {
                return Out;
            }
            int Index = DataList.FindIndex(x => Encoding.UTF8.GetString(x.Item1.ToArray()).Contains("SettID"));
            if (Index == -1)
            {
                return Out;
            }
            var Data = DataList[Index].Item2;
            if (Data.Count < 4)
            {
                return Out;
            }

            var TheArray = Data.ToArray();
            int HeadLength = BitConverter.ToUInt16(TheArray, 0);
            if (HeadLength < 4)
            {
                return Out;
            }
            int Version = BitConverter.ToUInt16(TheArray, 2);
            int Iterator = HeadLength;
            if (Version == 0)
            {

                while (true)
                {

                    int TheLength = BitConverter.ToUInt16(TheArray, Iterator);
                    if (TheLength < 14 || Iterator + TheLength > Data.Count)
                    {
                        break;
                    }
                    SettingFileIDInfo IDInfo = new SettingFileIDInfo();
                    IDInfo.Type = (SettingFileType)BitConverter.ToUInt16(TheArray, Iterator + 2);
                    IDInfo.width = BitConverter.ToUInt16(TheArray, Iterator + 4);
                    IDInfo.height = BitConverter.ToUInt16(TheArray, Iterator + 6);
                    IDInfo.ID = BitConverter.ToUInt16(TheArray, Iterator + 8);
                    IDInfo.RegionID = BitConverter.ToUInt16(TheArray, Iterator + 10);
                    IDInfo.IspClock = BitConverter.ToUInt16(TheArray, Iterator + 12);
                    Out.Add(IDInfo);
                    Iterator += TheLength;
                    if (Iterator >= Data.Count)
                    {
                        break;
                    }
                }
            }
            return Out;


        }

    }
}
